extends State
class_name PlayerStateJump

# 重构后的玩家跳跃状态 - 简化逻辑

var input_handler: InputHandler

func enter(_data: Dictionary = {}) -> void:
	# 获取输入处理器
	input_handler = character.get_node("InputHandler") as InputHandler
	if input_handler:
		input_handler.jump_pressed.connect(_on_jump_pressed)
		input_handler.movement_input.connect(_on_movement_input)
		input_handler.dash_pressed.connect(_on_dash_pressed)
	
	
	animation.play("pre_jump")

func exit() -> void:
	# 断开信号连接
	if input_handler:
		if input_handler.movement_input.is_connected(_on_movement_input):
			input_handler.movement_input.disconnect(_on_movement_input)
		if input_handler.dash_pressed.is_connected(_on_dash_pressed):
			input_handler.dash_pressed.disconnect(_on_dash_pressed)
		if input_handler.jump_pressed.is_connected(_on_jump_pressed):
			input_handler.jump_pressed.disconnect(_on_jump_pressed)

func update(_delta: float) -> void:
	_check_fall_transition()

func physics_update(_delta: float) -> void:
	# 物理更新由移动输入处理
	pass

# 处理移动输入
func _on_movement_input(direction: Vector2) -> void:
	if not character.can_move:
		return
	
	# 空中移动控制
	var target_velocity = direction.x * character.speed
	character.velocity.x = move_toward(
		character.velocity.x, 
		target_velocity, 
		character.acceleation * get_physics_process_delta_time() * 0.7
	)

# 跳跃输入
func _on_jump_pressed() -> void:
	if _can_jump():
		character.velocity.y = -character.jump_speed
		input_handler.consume_buffered_input("jump")
		MusicPlayer.play_sfx("Jump")
		character.can_jump = false

# 处理冲刺输入
func _on_dash_pressed() -> void:
	if character.can_dash:
		input_handler.consume_buffered_input("dash")
		change_state("dash")

# 检查是否应该转换到下落状态
func _check_fall_transition() -> void:
	if character.velocity.y > 0:
		change_state("fall")

# 获取状态数据
func get_state_data() -> Dictionary:
	return {
		"velocity": character.velocity,
		"is_ascending": character.velocity.y < 0
	}

func _can_jump() -> bool:
	return character.can_jump && input_handler.has_buffered_input("jump")

## 调试信息
#func get_debug_info() -> String:
	#return "Jump State - Velocity: %s, Ascending: %s" % [
		#character.velocity,
		#character.velocity.y < 0
	#]
